package com.lsk.mps.backend.aspect;

import com.lsk.mps.backend.util.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
@Order(0)
@Component
public class JsonReturnAspect {
    @Pointcut("@annotation(com.lsk.mps.backend.aspect.annotation.JsonReturn)")
    public void pointcut() {}

    @Around("pointcut()")
    public Object around(ProceedingJoinPoint pjp) {
        try {
            Object result = pjp.proceed();
            return ResponseUtil.buildSuccessResponse(result);
        } catch (Throwable t) {
            log.error("Caught error: ", t);
            return ResponseUtil.buildFailedResponse(t);
        }
    }

}
